﻿
@{
    ViewData["Title"] = "StorageUpdate";
    Layout = "~/Views/Home/Menu.cshtml";
}

<h1>入库修改</h1>
<form class="layui-form" action="">
	<h2>基本信息</h2>
	<div class="layui-form-item">
		<label class="layui-form-label">入库名称</label>
		<div class="layui-input-inline">
			<input type="text" name="storageName" id="storageName" autocomplete="off" class="layui-input">
		</div>
		<label class="layui-form-label">入库编号</label>
		<div class="layui-input-inline">
			<input type="text" name="storageNumbering" id="storageNumbering" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">*入库日期</label>
		<div class="layui-input-inline layui-input-wrap">
			<input type="date" name="storageDate" id="storageDate" autocomplete="off" lay-verify="required" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">备注</label>
		<div class="layui-input-block" style="width:600Px">
			<textarea name="storageRemark" id="storageRemark" placeholder="请输入内容" class="layui-textarea"></textarea>
		</div>
	</div>

	<h2>入库明细</h2>

	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">产品名称</label>
			<div class="layui-input-inline">
				<input type="text" name="detailsName" id="detailsName" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">产品编号</label>
			<div class="layui-input-inline layui-input-wrap">
				<input type="text" name="detailsNumbering" id="detailsNumbering" autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">规格型号</label>
			<div class="layui-input-inline">
				<input type="text" name="detailsSpec" id="detailsSpec" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">单位</label>
			<div class="layui-input-inline layui-input-wrap">
				<select name="detailsUnit" id="detailsUnit" lay-filter="aihao">
					<option value="">请选择</option>
					<option value="个">个</option>
					<option value="箱">箱</option>
					<option value="件">件</option>
					<option value="套">套</option>
					<option value="台">台</option>
				</select>
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">批次号</label>
			<div class="layui-input-inline">
				<input type="text" name="detailsNumber" id="detailsNumber" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">入库数量</label>
			<div class="layui-input-inline layui-input-wrap">
				<input type="text" name="detailsNum" id="detailsNum" autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>

	<div class="layui-form-item">

		<div class="layui-inline layui-col-xs5">
			<label class="layui-form-label">仓库名</label>
			<div class="layui-input-inline" style="width:120px">
				<select name="warehouseName" id="warehouseName" lay-filter="kuqu">
					<option value="">请选择</option>
					<option value="仓库一">仓库一</option>
					<option value="仓库二">仓库二</option>
					<option value="仓库三">仓库三</option>
				</select>
			</div>
			<div class="layui-input-inline" style="width:120px">
				<select name="reservoirName" id="reservoirName" lay-filter="kuwei">
					<option value="">请选择</option>
					<option value="库区一">库区一</option>
					<option value="库区二">库区二</option>
					<option value="库区三">库区三</option>
				</select>
			</div>
			<div class="layui-input-inline" style="width:120px">
				<select name="locationName" id="locationName">
					<option value="">请选择</option>
					<option value="库位一">库位一</option>
					<option value="库位二">库位二</option>
					<option value="库位三">库位三</option>
				</select>
			</div>
		</div>

	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">生产日期：</label>
		<div class="layui-input-inline layui-input-wrap">
			<input type="date" name="detailsDate" id="detailsDate" autocomplete="off" lay-verify="required" class="layui-input">
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">仓库负责人</label>
			<div class="layui-input-inline">
				<input type="text" name="warehouseHead" id="warehouseHead" autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>

	<div class="layui-form-item">

		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">修改</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>


<script>
	$(function(){
		 fantian()
	})
	var obj = {
		storageId:"",
		storageName:"",
		storageNumbering:"",
		storageDate:"",
		storageRemark:"",
		detailsName:"",
		detailsNumbering:"",
		detailsSpec: "",
		detailsUnit: "",
		detailsNumber: "",
		detailsNum: "",
		warehouseName: "",
		reservoirName: "",
		locationName: "",
		detailsDate: "",
		warehouseHead: "",
		addName: "",
		addTime: "",
		updateName: "",
		updateTime: "",
	}
	function fantian() {
		return new Promise(function (resolve, reject) {
			$(document).ready(function () {
				// 获取要编辑的用户ID，可以通过URL参数或者其他方式获取，这里假设通过URL参数获取
				var storageId = localStorage.getItem("storageId");
				console.log(storageId);
				// 如果获取到了用户ID，就去后台获取该用户的详细信息并反填到表单
				if (storageId) {
					$.ajax({
						type: "get",
						url: "/Storages/StoragesMondel",
						data: {
							id: storageId
						},
						success: function (res) {
							var resse = JSON.parse(res)
							console.log(resse)
							if (resse.data) {
								const s = resse.data;
								//反填日期框
								var da = new Date(s.storageDate);
								// 获取年、月、日
								var year = da.getFullYear();
								var month = (da.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
								var day = da.getDate().toString().padStart(2, '0');  // 补充两位数字
								// 拼接成 "yyyy-MM-dd" 格式
								var formattedDate = `${year}-${month}-${day}`;

								//反填日期框
								var da = new Date(s.detailsDate);
								// 获取年、月、日
								var year = da.getFullYear();
								var month = (da.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
								var day = da.getDate().toString().padStart(2, '0');  // 补充两位数字
								// 拼接成 "yyyy-MM-dd" 格式
								var detailstdDate = `${year}-${month}-${day}`;
								$('#storageId').val(s.storageId);
								$('#storageName').val(s.storageName);
								$('#storageNumbering').val(s.storageNumbering);
								$('#storageDate').val(formattedDate);
								$('#storageRemark').val(s.storageRemark);
								$('#detailsName').val(s.detailsName);
								$('#detailsNumbering').val(s.detailsNumbering);
								$('#detailsSpec').val(s.detailsSpec);
								$('#detailsUnit').val(s.detailsUnit);
								layui.form.render('select')
								$('#detailsNumber').val(s.detailsNumber);
								$('#detailsNum').val(s.detailsNum);
								$('#warehouseName').val(s.warehouseName);
								layui.form.render('select')
								$('#reservoirName').val(s.reservoirName);
								layui.form.render('select')
								$('#locationName').val(s.locationName);
								layui.form.render('select')
								$('#detailsDate').val(detailstdDate);
								$('#warehouseHead').val(s.warehouseHead);
								$('#addName').val(s.addName); 
								$('#addTime').val(s.addTime);
								obj.storageId = s.storageId,
									obj.storageName = s.storageName,
									obj.storageNumbering = s.storageNumbering,
									obj.storageDate = s.storageDate,
									obj.storageRemark = s.storageRemark,
									obj.detailsName = s.detailsName,
									obj.detailsNumbering = s.detailsNumbering,
									obj.detailsSpec = s.detailsSpec,
									obj.detailsUnit = s.detailsUnit,
									obj.detailsNumber = s.detailsNumber,
									obj.detailsNum = s.detailsNum,
									obj.warehouseName = s.warehouseName,
									obj.reservoirName = s.reservoirName,
									obj.locationName = s.locationName,
									obj.detailsDate = s.detailsDate,
									obj.warehouseHead = s.warehouseHead,
									obj.addName = s.addName,
									obj.addTime = s.addTime
									console.log(obj)
							}
						}
					})
				}
			});
		});
	};
	var obj1 = {
		storageId: "",
		storageName: "",
		storageNumbering: "",
		storageDate: "",
		storageRemark: "",
		detailsName: "",
		detailsNumbering: "",
		detailsSpec: "",
		detailsUnit: "",
		detailsNumber: "",
		detailsNum: "",
		warehouseName: "",
		reservoirName: "",
		locationName: "",
		detailsDate: "",
		warehouseHead: "",
		addName: "",
		addTime: "",
		updateName: ""
	}
	console.log(obj1)
	layui.use(['layer', 'form', 'util'], function () {
		var layer = layui.layer;
		var $ = layui.jquery;
		var form = layui.form;
		var util = layui.util;

		form.on('submit(formDemo)', function (data) {
			var field = data.field; // 获取表单字段值
			obj1.storageId = obj.storageId,
				obj1.storageName = field.storageName,
				obj1.storageNumbering = field.storageNumbering,
				obj1.storageDate = field.storageDate,
				obj1.storageRemark = field.storageRemark,
				obj1.detailsName = field.detailsName,
				obj1.detailsNumbering = field.detailsNumbering,
				obj1.detailsSpec = field.detailsSpec,
				obj1.detailsUnit = field.detailsUnit,
				obj1.detailsNumber = field.detailsNumber,
				obj1.detailsNum = field.detailsNum,
				obj1.warehouseName = field.warehouseName,
				obj1.reservoirName = field.reservoirName,
				obj1.locationName = field.locationName,
				obj1.detailsDate = field.detailsDate,
				obj1.warehouseHead = field.warehouseHead,
				obj1.addName = obj.addName,
				obj1.addTime = obj.addTime,
			obj1.updateName="张三",
				console.log(obj1)
			// 显示填写结果，仅作演示用
			// 此处可执行 Ajax 等操作
			debugger
			$.ajax({
				url: '/Storages/StoragesUpdate',
				type: 'post',
				data: { dto: obj1, token: localStorage.getItem("token") },
				success: function (res) {
					var res = JSON.parse(res)
					console.log(obj1);
					console.log(res);
					if (res.data == 1) {
						layer.msg("修改成功", { icon: 1 })
						setTimeout(function () {
							location.href = '/Storages/StorageShow'
						}, 2000);
					} else {
						layer.msg("修改失败:" + res.message, { icon: 2 });
					}
				}
			});

			return false; // 阻止默认 form 跳转
		});
		// 对弹层中的表单进行初始化渲染
		form.render();
	});
</script>